#include "cellaccess.h"

namespace visualization
{
	/** The origonal is like the line commented below. Note that the order is checked when "-Wall" is used 
	 * while compiling.
	 *CellAccess::CellAccess(UnstructuredGrid *grid, size_t id) : server(grid), cell_id(id), current_cell(NULL), total(server->getCellOffsetSize())
	 */
CellAccess::CellAccess(UnstructuredGrid *grid, size_t id)
            : server(grid), total(grid->getCellOffsetSize()), 
			cell_id(id), current_cell(NULL)
{
	tet = new Tetrahedron(this);
	hex = new Hexahedron(this);
	pry = new Pyramid(this);
	pri = new Prism(this);
	id_mapper.server = this;
	point_num = server->getCellType(cell_id) & 0x000000ff;
	offset = server->getCellOffsets(cell_id);
	cells = server->getCell();
	GetCurrentCellHandler();
}

CellAccess::~CellAccess()
{
	delete tet;
	delete hex;
	delete pry;
	delete pri;
}

size_t CellAccess::PointIDMapper::operator()(size_t &dest) const
{
	return (*server->cells)[dest + server->offset];
}

Point * CellAccess::GetPoint(size_t i)
{
	return i >= point_num ? 0 : server->getPoint(id_mapper(i));
}

//CellType CellAccess::GetFacet(size_t i, size_t *point_ids)
//{
//	CellType ct = current_cell->GetFacet(i, point_ids);
//	
//	for (int pn=0; pn<(ct&0x000000ff); pn++)
//		point_ids[pn] = id_mapper(point_ids[pn]);
//	return ct;
//}

bool CellAccess::Sycronize()
{
/*	if (cell_id >= total)
	{
		offset = total;
		point_num = 0;
		current_cell = 0;
		return false;
	}
	offset = server->getCellOffsets(cell_id);
	point_num = server->getCellType(cell_id) & 0x000000ff;
	GetCurrentCellHandler();
	*/
	return true;
}

void CellAccess::GetCurrentCellHandler()
{
/*	size_t type = server->getCellType(cell_id);
	switch (type)
	{
	case TETRAHEDRON:
		current_cell = tet;
		break;
	case HEXAHEDRON:
		current_cell = hex;
		break;
	case PYRAMID:
		current_cell = pry;
		break;
	case PRISM:
		current_cell = pri;
		break;
	default:
		current_cell = 0;
		break;
	}
*/
}

}

